package com.xlserver.controller;

import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xlserver.dao.StateMapper;
import com.xlserver.domain.State;
import com.xlserver.service.MessageService;
import com.xlserver.service.SupervisorService;
import com.xlserver.util.ImageParser;
import com.xlserver.util.SMSUtils;
import com.xlserver.util.model.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.util.Map;

@RestController
@RequestMapping("/supervisor")
@AllArgsConstructor
@Api(value = "部分督导及密码登录相关模块接口", tags = "部分督导及密码登录相关模块接口")
public class SupervisorController {
    @Autowired
    public SMSUtils smsUtils;

    @Autowired
    public ImageParser parser;

    @Autowired
    public SupervisorService supervisorService;

    @Autowired
    StateMapper stateMapper;

    @Autowired
    MessageService messageService;

    /**
     * 修改督导个人信息
     * id，头像，姓名，性别，年龄，身份证，电话，邮箱
     */
    @PostMapping("/updateInfo")
    @ApiOperation(value = "修改督导个人信息", notes = "userInfo")
    public JSONObject updateInfo(@ModelAttribute UserInfo userInfo) throws IOException {
        JSONObject res = new JSONObject();
        userInfo.setPhotoAddress(ImageParser.saveImage(userInfo.getProfilePhoto()));
        if (supervisorService.updateInfo(userInfo)){
            res.put("result","success");
            res.put("avatar",userInfo.getPhotoAddress());
        }
        else{
            res.put("result","failure");
        }
        return res;
    }
    /**
     * 密码登录
     * @return 1.无user 2.密码错误 3.成功
     */
    @PostMapping("/login")
    @ApiOperation(value = "密码登录", notes = "{username:username,password:password}")
    public JSONObject login(@RequestBody Map<String, String> info, HttpServerRequest request, HttpServerResponse response) throws ParseException {
        String username = info.get("username");
        String password = info.get("password");
        return supervisorService.getUserInfo(username,password);
    }
    /**
     * 登出
     */
    @PostMapping("/logout")
    public JSONObject logout(@RequestBody Map<String,String> info){
        int id = Integer.parseInt(info.get("id"));
        stateMapper.update(null,new UpdateWrapper<State>().eq("id",id)
                .set("state",-1));
        JSONObject ret = new JSONObject();
        ret.put("result","success");
        return ret;
    }
    /**
     * 获取督导所有会话
     */
    @PostMapping("getAllMsgs")
    @ApiOperation(value = "获取督导所有会话", notes = "{id=id}")
    public JSONArray getAllmsgs(@RequestBody Map<String,String> info, HttpServerRequest request, HttpServerResponse response){
        String id = info.get("id");
        return supervisorService.getAllMsgs(Integer.parseInt(id));
    }
    /**
     * 督导正在进行的会话数
     */
    @PostMapping("getSumOfChats")
    public JSONObject getSumOfChats(@RequestBody Map<String,String> info){
        int id = Integer.parseInt(info.get("id"));
        return messageService.getSumOfChats(id);
    }
    /**
     * 根据咨询师姓名查询督导所有会话
     */
    @PostMapping("getAllMsgsByName")
    @ApiOperation(value = "根据咨询师姓名查询督导所有会话", notes = "{id:id,name:name}")
    public JSONArray getAllMsgsByName(@RequestBody Map<String,String> info, HttpServerRequest request, HttpServerResponse response){
        String id = info.get("id");
        String name = info.get("name");
        return supervisorService.getAllMsgsByName(Integer.parseInt(id),name);
    }
    /**
     * 获取督导信息
     * 头像，姓名，性别，年龄，身份证，电话，邮箱
     */
    @PostMapping("getInfo")
    public JSONObject getInfo(@RequestBody Map<String,String> info){
        int id = Integer.parseInt(info.get("id"));
        return supervisorService.getCounselorInfo(id);

    }
    /**
     * 通过会话id获取与咨询师详细消息记录
     */
    @PostMapping("getMessages")
    public JSONArray getMessages(@RequestBody Map<String,String> info){
        int hid = Integer.parseInt(info.get("hid"));
        return messageService.getMsgsOfChat(hid);
    }
    /**
     * 根据cid获取咨询师与访客正在进行的会话
     */
    @PostMapping("getOngoingSession")
    public JSONArray getOngoingSession(@RequestBody Map<String,String> info){
        int id = Integer.parseInt(info.get("id"));
        return messageService.getOnGoingChat(id);
    }





}
